package com.brave.mvvm.mvvmhelper.utils

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import com.blankj.utilcode.util.Utils
import com.brave.mvvm.mvvmhelper.base.CommonApp

/**
 * ***author*** ：brave tou
 *
 * ***data***   : 2021/9/1 15:02
 *
 * ***desc***   : Drawable工具
 */

/**
 * 获取Drawable
 */
fun Context?.getDrawable(@DrawableRes resId: Int): Drawable? {
    val context = this
        ?: CommonApp.default.applicationContext
        ?: Utils.getApp().applicationContext
    return ContextCompat.getDrawable(context, resId)
}

/**
 * Drawable染色
 */
fun Drawable?.dyeing(@ColorInt color: Int): Drawable {
    if (null == this) return ColorDrawable(color)
    val wrap = DrawableCompat.wrap(this)
    DrawableCompat.setTintList(wrap, ColorStateList.valueOf(color))
    return wrap
}